package com.wanmait.movie.webController;
import com.wanmait.movie.entity.Actor;
import com.wanmait.movie.entity.Movie;
import com.wanmait.movie.entity.Participating;
import com.wanmait.movie.entity.VO.MovieDTO;
import com.wanmait.movie.service.*;
import com.wanmait.movie.util.AjaxResult;
import com.wanmait.movie.util.UploadFileUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("web/movie")
public class WebMovieController {
    @Resource
    private ParticipatingService participatingService;
    @Resource
    CommentsService commentsService;
    @Resource
    private MovieService movieService;
    @Resource
    private MovieTypeService movieTypeService;
    @Resource
    private MovieTypesService movieTypesService;
    @Resource
    private UploadFileUtil uploadFileUtil;
    @Resource
    private ActorService actorService;

    @GetMapping("list")
    public AjaxResult list(Integer pageNum, Integer movieTypeId, String keyword){
        if (pageNum==null||pageNum==0){
            pageNum=1;
        }
        if (movieTypeId==null){
            movieTypeId=0;
        }
        if (keyword==null){
            keyword="";
        }
        return AjaxResult.success(movieService.getESMovieList(pageNum, movieTypeId, keyword)).put("movieType",movieTypeService.getMovieTypeList("")).put("total",movieService.getESMovieList(pageNum, movieTypeId, keyword).getTotal());
    }
    @GetMapping("lists")
    public AjaxResult lists(Integer pageNum, Integer movieId, String keyword) {
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (movieId == null) {
            movieId = 0;
        }
        if (keyword == null) {
            keyword = "";
        }
        return AjaxResult.success(commentsService.getCommentsList(pageNum, movieId, keyword)).put("total", commentsService.getCommentsList(pageNum, movieId, keyword).getTotal());
    }
    @GetMapping("finddirectorbymovieid")
    public AjaxResult finddirectorbymovieid(Integer movieId){
        System.out.println(movieId);
       String name=actorService.getDirector(movieService.getById(movieId).getActorDirector());
       return AjaxResult.success(name);
    }
    @GetMapping("findmovieTypebymovieid")
    public AjaxResult findmovieTypebymovieid(Integer id){
        List<Integer> movieTypeList=movieTypesService.getMovieTypeListByMovieId(id);
       List<String> list=new ArrayList<>();
       for (int i = 0; i < movieTypeList.size(); i++) {
            list.add( movieTypeService.getById(movieTypeList.get(i)).getMovieTypeName());
        }
        return AjaxResult.success(list);
    }
    @GetMapping("findactsbymovieid")
    public AjaxResult findactsbymovieid(Integer id){
        List<Participating> actList=participatingService.getParticipatingByMovieId(id);
        List<String> list=new ArrayList<>();
        for (int i = 0; i < actList.size(); i++) {
            list.add(actorService.getById(actList.get(i).getActorId()).getName());
        }
        return  AjaxResult.success(list);
    }
    @GetMapping("findMovieById")
    public AjaxResult findMovieById(Integer id){
        Movie movie = movieService.getById(id);
        List<Integer> movieTypeList = movieTypesService.getMovieTypeListByMovieId(id);
        MovieDTO movieDTO = new MovieDTO();
        movieDTO.setId(id).setMovieName(movie.getMovieName()).setMoviePic(movie.getMoviePic())
                .setActorDirector(movie.getActorDirector()).setIntroduction(movie.getIntroduction())
                .setLanguage(movie.getLanguage()).setMovieTypeList(movieTypeList).setEndTime(movie.getEndTime())
                .setReleaseTime(movie.getReleaseTime()).setSource(movie.getSource()).setTime(movie.getTime());
        return AjaxResult.success(movieDTO);
    }

//    @GetMapping("getMovieTime")
//    public AjaxResult getMovieTime(Integer id){
//        return AjaxResult.success(movieService.getMovieTime(id));
//    }
}

